package CustomUI;

import gui.AddEditObjects;
import gui.Assemble;
import gui.EditParts;
import gui.Objects;

import java.util.ArrayList;
import java.util.List;

import org.Consts;
import org.Globals;
import org.Utility;
import org.json.JSONException;
import org.json.JSONObject;

import com.ydev.graphics.animations.R;

import DTO.FavouriteColorDTO;
import DTO.ObjectDTO;
import DTO.PartDTO;
import android.R.bool;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnLongClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AddPartsDialog extends Dialog {

	
	LinearLayout LLPartsHolder;
	ArrayList<LinearLayout> favColorsRows = new ArrayList<LinearLayout>();
	List<View> allViews;
	Activity mActivity;
	ArrayList<PartDTO> partsList = new ArrayList<PartDTO>();

	public AddPartsDialog(Context context, Activity act) {
		super(context);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.add_part_dialog);
		this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		mActivity=act;
		setupScreen();
	}
	
	private Handler mHandler = new Handler();

	public void setupScreen() {

		//svFavColorsView = (ScrollView) findViewById(R.id.scrollViewFavColors);
		LLPartsHolder = (LinearLayout) findViewById(R.id.LLPartsHolder);

		// loadFavColors();
		//loadFavColors2();

		// new loadFavAsync().execute();

		mHandler.postDelayed(runnable, 1);

	}

	private Runnable runnable = new Runnable() {
		public void run() {
			allViews = new ArrayList<View>();
			
			ArrayList<ObjectDTO> imageList = ObjectDTO
					.getObjectsInDatabase(Globals.sqlDB);

			LinearLayout imageLayout = new LinearLayout(getContext());
			imageLayout.setOrientation(LinearLayout.HORIZONTAL);
			imageLayout.setLayoutParams(new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));

			if (imageList != null) {
				View v = null;
				for (int i = 0; i < imageList.size(); i++) {
					allViews.add(inflateContents(imageList.get(i), i));
				}

			} else {
				Log.d("", "== image list was null");
			}

			try {

				if (LLPartsHolder.getChildCount() > 0)
					LLPartsHolder.removeAllViews();

				/*
				 * Display display = getWindowManager() .getDefaultDisplay();
				 */
				int maxWidth = LLPartsHolder.getWidth();

				LinearLayout newLL = new LinearLayout(getContext());
				newLL.setLayoutParams(new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				newLL.setGravity(Gravity.LEFT);
				newLL.setOrientation(LinearLayout.HORIZONTAL);

				int widthSoFar = 0;
				int imageSize = maxWidth / 4 - 3;
				LayoutParams lp = new LayoutParams(imageSize, imageSize);
				LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
						imageSize, imageSize);
				par.setMargins(10, 0, 0, 10);
				for (int i = 0; i < allViews.size(); i++) {

					allViews.get(i).measure(0, 0);

					widthSoFar += imageSize;// allViews.get(i).getMeasuredWidth();

					Log.d("", "== widthSoFar is " + String.valueOf(widthSoFar));
					Log.d("", "== maxWidth is " + String.valueOf(maxWidth));
					if (newLL.getChildCount()==3) {
						LLPartsHolder.addView(newLL);
						Log.d("", "== a new row");

						newLL = new LinearLayout(getContext());
						newLL.setLayoutParams(new LayoutParams(
								LayoutParams.FILL_PARENT,
								LayoutParams.WRAP_CONTENT));
						newLL.setOrientation(LinearLayout.HORIZONTAL);
						newLL.setGravity(Gravity.LEFT);

						widthSoFar = 0;

						newLL.addView(allViews.get(i), par);

						allViews.get(i).measure(0, 0);
						widthSoFar += allViews.get(i).getMeasuredWidth();
					} else {
						newLL.addView(allViews.get(i), par);
					}
				}

				LLPartsHolder.addView(newLL);

			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
		}
		public View inflateContents(final ObjectDTO obj, final int index) {

			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final View cell = inflater.inflate(R.layout.gallery_cell, null, true);
			cell.setTag(index);
			ImageView imgPicture = (ImageView) cell
					.findViewById(R.id.imgSmallPicture);

			if (obj != null) {
				imgPicture.setImageBitmap(obj.image);
			} else {
				imgPicture.setImageDrawable(Utility.getDrawable(
						getContext(), R.drawable.no_photo, false));
			}

			cell.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			JSONObject tag = new JSONObject();
			try {
				tag.put(Consts.COLUMN_OBJECT_ID, obj.intUuid);
				
				tag.put("isSelected", "false");
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			cell.setTag(tag.toString());
			cell.setTag(index);
			cell.setOnClickListener(new View.OnClickListener() {
				int _id=obj.intUuid;
				@Override
				public void onClick(View v) {

					Intent intent =new Intent(mActivity, EditParts.class);
					
					intent.putExtra("partImage",_id+"");
					mActivity.startActivityForResult(intent, 3);
					((Assemble)mActivity).apd.hide();
					
				}
			});


			return cell;
		}

	};

	

}
